package com.edu.test;

import com.edu.bean.Book;
import com.edu.mapper.BookMapper;
import com.edu.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/*
 * @Author:mx
 * @Date:2025/10/15-9:01:17
 */
public class MyTest02 {

    private SqlSession session1 ;

    private SqlSession session2 ;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(path);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        session1 = sessionFactory.openSession();
        session2 = sessionFactory.openSession();
    }

    /*
    *   二级缓存 生效的条件
    *   1.开启了二级缓存
    *   2.XxxxxMapper.xml中 加入 <cache></cache>
    *   3.查询的数据 要实现 Serializable接口
    *   4.执行完查询之后，只要sqlSession去commit 或者close的时候，才会把数据存入到二级缓存
    *
    * */
    @Test
    public void test01(){
        BookMapper bookMapper1 = session1.getMapper(BookMapper.class);
        BookMapper bookMapper2 = session2.getMapper(BookMapper.class);
        Book book = bookMapper1.selectBookById(1); // 查到的结果 会存入到 session1的 一级缓存
        session1.commit(); // 提交时 把之前查到的数据 存入到 BookMapper的二次缓存
        Book book2 = bookMapper2.selectBookById(1); //
        Book book3 = bookMapper2.selectBookById(1);
        Book book4 = bookMapper2.selectBookById(1);
        System.out.println(book2);
    }

    /*
    *   二级缓存失效的条件
    *       1. 同一个 mapper 执行了 增删改 ，当前mapper的二级缓存 立刻被清除
    * */

    @Test
    public void test02(){
        BookMapper bookMapper1 = session1.getMapper(BookMapper.class);
        BookMapper bookMapper2 = session2.getMapper(BookMapper.class);
        Book book = bookMapper1.selectBookById(1); // 查到的结果 会存入到 session1的 一级缓存
        session1.commit(); // 提交时 把之前查到的数据 存入到 BookMapper的二次缓存
        Book book2 = bookMapper2.selectBookById(1); // 从二级缓存查到了结果
        System.out.println(book2);
        bookMapper1.deleteBookById(1);
        session1.commit();//由于 BookMapper 执行了 删除操作，所以BookMapper的 二级缓存立即被清空
        Book book3 = bookMapper2.selectBookById(1); //重新查数据库
        System.out.println(book3);
    }


    @Test
    public void test03(){
        BookMapper bookMapper1 = session1.getMapper(BookMapper.class);
        BookMapper bookMapper2 = session2.getMapper(BookMapper.class);
        Book book = bookMapper1.selectBookById(1); // 存入 session1 的一级缓存
        System.out.println("-------------");
        Book book0 = bookMapper2.selectBookById(1); // 存入 session2 的一级缓存
        System.out.println("-------------");
        session1.commit(); // 把79的查询 结果 存入 BookMapper的 2级缓存
        Book book2 = bookMapper2.selectBookById(1); // 查BookMapper的 二级缓存  33%
        System.out.println("-------------");
        bookMapper1.deleteBookById(1);//删除
        session1.commit();// BookMapper 的所有 二级缓存 清空
        System.out.println("-------------");
        Book book3 = bookMapper2.selectBookById(1);// 查2号的一级缓存 25%
        System.out.println(book3);
    }


    @Test
    public void test04(){
        BookMapper bookMapper1 = session1.getMapper(BookMapper.class);
        BookMapper bookMapper2 = session2.getMapper(BookMapper.class);
        UserMapper userMapper = session1.getMapper(UserMapper.class);

        Book book = bookMapper1.selectBookById(1);
        session1.commit();

        Book book2 = bookMapper2.selectBookById(1);
        userMapper.deleteUserById(1);
        session1.commit();//

        Book book3 = bookMapper2.selectBookById(1);
        System.out.println(book3);
    }


    @Test
    public void test05() throws InterruptedException {
        BookMapper bookMapper1 = session1.getMapper(BookMapper.class);
        BookMapper bookMapper2 = session2.getMapper(BookMapper.class);

        Book book = bookMapper1.selectBookById(1);
        session1.commit();

        Book book2 = bookMapper2.selectBookById(1);
        Thread.sleep(2000);
        Book book3 = bookMapper2.selectBookById(1);
        System.out.println(book3);
    }

}
